package com.example.rice.controller;
import com.example.rice.common.R;
import com.example.rice.service.impl.OSSUploadServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


public class OSSController {

    @Resource
    private OSSUploadServiceImpl ossUploadService;

    @PostMapping("/upload")
    public R uploadfile(@RequestParam("file") MultipartFile multipartFile){
        if (multipartFile.isEmpty()){
            return R.error("文件错误!");
        }
        Long size = multipartFile.getSize();
        String originalFilename = multipartFile.getOriginalFilename();
        String contentType = multipartFile.getContentType();
        if (contentType.equals("png|jpg|gif|jpeg")){
            return R.error("文件类型不正确!");
        }
        return R.success(ossUploadService.uploadfile(multipartFile));
    }

    /**
     *
     * @param name
     * @param response
     * @throws IOException
     */
    @GetMapping("/download")
    public R<String> download(String name, HttpServletResponse response) throws IOException {
        return R.success(name);
    }

}
